package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.ajax.AjaxRes;
import com.controller.base.BaseController;
import com.entity.ChanPing;
import com.entity.XinWenZiXun;
import com.entity.YongHu;
import com.entity.YongHuHangYe;
import com.service.CpService;
import com.service.UserIndustryService;
import com.service.UserService;
import com.service.XwzxService;
import com.util.Md5;

@Controller
public class InsexpageController extends BaseController<Object>{
	@Autowired
	private UserIndustryService yhhyService;
	@Autowired
	private UserService yhService;
	@Autowired
	private XwzxService xwzxService;	
	@Autowired
	private CpService cpService;	
	
	
	//跳转主页
	@RequestMapping("/indexpage")
	public String index(HttpServletRequest request,Model md)throws Exception{
	HttpSession yhSession =request.getSession();
	YongHu yh=(YongHu)yhSession.getAttribute("yh");
	md.addAttribute("yhs",yh);
		return "index";
	}
	
	
	@RequestMapping("/testcontent")
	public ModelAndView testcontent(){
		ModelAndView mv =  new ModelAndView();
		mv.setViewName("pages/content");
		return mv;
	}
	@RequestMapping("/cp")
	public ModelAndView cp(){
		ModelAndView mv =  new ModelAndView();
		mv.setViewName("pages/cp");
		return mv;
	}
	@RequestMapping("/usertable")
	public ModelAndView usertable(){
		ModelAndView mv =  new ModelAndView();
		mv.setViewName("pages/user");
		return mv;
	}
	@RequestMapping("/testuser")
	public ModelAndView testuser(){
		ModelAndView mv =  new ModelAndView();
		mv.setViewName("pages/usertable");
		return mv;
	}
	@RequestMapping("/table")
	public ModelAndView table(){
		ModelAndView mv =  new ModelAndView();
		mv.setViewName("pages/tables");
		return mv;
	}
	@RequestMapping("/xwzx")
	public ModelAndView xwzx(){
		ModelAndView mv =  new ModelAndView();
		mv.setViewName("pages/xwzx");
		return mv;
	}
	@RequestMapping("/login")
	public ModelAndView login(Model md){
		ModelAndView mv =  new ModelAndView();
			mv.setViewName("pages/login");
			return mv;
	}
	@RequestMapping("/to_login")
	@ResponseBody
	public AjaxRes toLogin(HttpServletRequest request,YongHu yh){
		AjaxRes res=new AjaxRes();
		// String pwd = Md5.getPwd(yh.getPhone());
	//	 yh.setPhone(pwd);
		int i=yhService.countPWD(yh);
		System.out.println(yh.getPhone());
		if(i<=0){
			res.setFailMsg("登陆失败");
			return res;
		}
			res.setSucceedMsg("登陆成功");
			List<YongHu> yhlist=yhService.find(yh);
			request.getSession().setAttribute("yh", yhlist.get(0));
			return res;

	}
	@RequestMapping("/to_logout")
	@ResponseBody
	public ModelAndView toLogout(HttpServletRequest request,YongHu yh){
		ModelAndView mv =  new ModelAndView();
		HttpSession userSession =request.getSession();
		userSession.setAttribute("yh",null);
		mv.setViewName("pages/login");
		return mv;
	}
	@RequestMapping("/addyhhy")
	public String addyhhy(Model md,String id){
		YongHuHangYe o = new YongHuHangYe();
		o.setId(id);
		List<YongHuHangYe> yhList = yhhyService.find(o);
		if(yhList.size()>0){
			o=yhList.get(0);
		}
		md.addAttribute("yhhy",o);
		return "pages/addyhhy";
	}
	
	@RequestMapping("/updateyhhy")
	public String updateyhhy(Model md,String id){
		System.out.println("id"+id);
		YongHuHangYe o = new YongHuHangYe();
		o.setId(id);
		System.out.println(o);
		List<YongHuHangYe> yhList = yhhyService.find(o);
		if(yhList.size()>0){
			o=yhList.get(0);
		}
		md.addAttribute("yhhy",o);
		return "pages/updateyhhy";
	}
	@RequestMapping("/addyh")
	public String addyh(Model md,String id){
		YongHu o = new YongHu();
		o.setId(id);
		List<YongHu> yhList = yhService.find(o);
		if(yhList.size()>0){
			o=yhList.get(0);
		}
		md.addAttribute("yh",o);
		return "pages/adduser";
	}
	@RequestMapping("/addcp")
	public String addcp(Model md,String id,HttpServletRequest request){
		ChanPing o = new ChanPing();
		HttpSession yhSession =request.getSession();
		YongHu yh=(YongHu)yhSession.getAttribute("yh");
		md.addAttribute("yhs",yh);
		o.setId(id);
		List<ChanPing> cpList = cpService.find(o);
		if(cpList.size()>0){
			o=cpList.get(0);
		}
		md.addAttribute("cp",o);
		return "pages/addcp";
	}
	@RequestMapping("/updateyh")
	public String updateyh(Model md,String id){
		System.out.println("id"+id);
		YongHu o = new YongHu();
		o.setId(id);
		System.out.println(o);
		List<YongHu> yhList = yhService.find(o);
		if(yhList.size()>0){
			o=yhList.get(0);
		}
		md.addAttribute("yh",o);
		return "pages/updateuser";
	}
	@RequestMapping("/updatecp")
	public String updatecp(Model md,String id){
		System.out.println("id"+id);
		ChanPing o = new ChanPing();
		o.setId(id);
		System.out.println(o);
		List<ChanPing> cpList = cpService.find(o);
		if(cpList.size()>0){
			o=cpList.get(0);
		}
		md.addAttribute("cp",o);
		return "pages/upcp";
	}
	@RequestMapping("/updatexwzx")
	public String updatexwzx(Model md,String id){
		System.out.println("id"+id);
		XinWenZiXun o = new XinWenZiXun();
		o.setId(id);
		System.out.println(o);
		List<XinWenZiXun> xwzxList = xwzxService.find(o);
		if(xwzxList.size()>0){
			o=xwzxList.get(0);
		}
		md.addAttribute("xwzx",o);
		return "pages/upxwzx";
	}
	@RequestMapping("/addxwzx")
	public String addxwzx(Model md,String id,HttpServletRequest request){
		XinWenZiXun o = new XinWenZiXun();
		HttpSession yhSession =request.getSession();
		YongHu yh=(YongHu)yhSession.getAttribute("yh");
		md.addAttribute("yhs",yh);
		o.setId(id);
		List<XinWenZiXun> yhList = xwzxService.find(o);
		if(yhList.size()>0){
			o=yhList.get(0);
		}
		md.addAttribute("xwzx",o);
		return "pages/addxwzx";
	}
	
	
}
